<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Broadcast\Repository\Broadcast_execution_log;

use Plugin\Attractions\Broadcast\Model\Broadcast_execution_log\BroadcastExecutionLog;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 广播执行日志 Repository类
 */
class BroadcastExecutionLogRepository extends IRepository
{
   public function __construct(
        protected readonly BroadcastExecutionLog $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 计划ID
        if (isset($params['plan_id']) && !empty($params['plan_id'])) {
            $query->where('plan_id', '=', $params['plan_id']);
        }

        // 执行类型：1-计划执行，2-手动执行，3-应急执行
        if (isset($params['execution_type']) && !empty($params['execution_type'])) {
            $query->where('execution_type', '=', $params['execution_type']);
        }

        // 广播内容
        if (isset($params['broadcast_content']) && !empty($params['broadcast_content'])) {
            $query->where('broadcast_content', '=', $params['broadcast_content']);
        }

        // 开始播放时间
        if (isset($params['start_time']) && !empty($params['start_time']) && is_array($params['start_time']) && count($params['start_time']) == 2) {
            $query->whereBetween(
                'start_time',
                [ $params['start_time'][0], $params['start_time'][1] ]
            );
        }

        // 结束播放时间
        if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time']) && count($params['end_time']) == 2) {
            $query->whereBetween(
                'end_time',
                [ $params['end_time'][0], $params['end_time'][1] ]
            );
        }

        // 实际执行时长（秒）
        if (isset($params['actual_duration']) && !empty($params['actual_duration'])) {
            $query->where('actual_duration', '=', $params['actual_duration']);
        }

        // 执行状态：1-执行中，2-已完成，3-失败
        if (isset($params['execution_status']) && !empty($params['execution_status'])) {
            $query->where('execution_status', '=', $params['execution_status']);
        }

        // 失败原因
        if (isset($params['fail_reason']) && !empty($params['fail_reason'])) {
            $query->where('fail_reason', 'like', '%'.$params['fail_reason'].'%');
        }

        // 播放音量
        if (isset($params['volume_level']) && !empty($params['volume_level'])) {
            $query->where('volume_level', '=', $params['volume_level']);
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        return $query;
    }
}